package com.origin.ebuy.utils;

/**
 * 
 * sql����װ
 * @author Administrator
 */
public class SQLUtils {
	
	/**
	 * ƴ�Ӳ�ѯ���
	 * @param table
	 * @param cols
	 * @param where
	 * @return
	 */
	public static String getQuerySQL(String table,String[] cols,String where){
		//SELECT money,id FROM account where id=1;
		StringBuilder builder = new StringBuilder("SELECT ");
		for (String col : cols) {
			builder.append(col);
			builder.append(",");
		}
		builder.deleteCharAt(builder.length()-1);
		builder.append(" from ");
		builder.append(table);
		if(where != null && where.trim().length()!=0){
			builder.append(" where ");
			builder.append(where);
		}
		return builder.toString();
	}
	
	/**
	 * ƴ��update���
	 * @param table �����
	 * @param cols �����
	 * @param where ����
	 * @return
	 */
	public static String getUpdateSQL(String table,String[] cols,String where){
		//update account set money=2000,name=111 where id=2;
		StringBuilder builder = new StringBuilder("update ");
		builder.append(table);
		builder.append(" set ");
		for (String string : cols) {
			builder.append(string);
			builder.append("=? ,");
		}
		builder.deleteCharAt(builder.length()-1);
		builder.append(" where ");
		builder.append(where);
		return builder.toString();
	}
	
	/**
	 * ƴ�Ӳ������
	 * @param table �����
	 * @param cols �����
	 * @return
	 */
	public static String getInsertSQL(String table,String[] cols){
		//insert into account (uname,money) values(?,?);
		StringBuilder builder = new StringBuilder("insert into ");
		builder.append(table);
		builder.append(" (");
		//ƴ��(uname,money)
		for (String col : cols) {
			builder.append(col);
			builder.append(",");
		}
		builder.deleteCharAt(builder.length()-1);
		builder.append(") values (");
		//ƴ��(?,?)
		for (String col : cols) {
			builder.append("?,");
		}
		builder.deleteCharAt(builder.length()-1);
		builder.append(")");
		return builder.toString();
	}
	
}
